
package eSchool.GUI.Controls;

import eSchool.Utils.TextHelper;
import javax.swing.JOptionPane;
import javax.swing.table.TableModel;

public class panEditDisciplina extends JPanelExtended {
      
    public panEditDisciplina() {
        initComponents();
    }  

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblIdDisciplina = new javax.swing.JLabel();
        txtIdDisciplina = new javax.swing.JTextField();
        txtDisciplina = new javax.swing.JTextField();
        lblDisciplina = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        lblDocente = new javax.swing.JLabel();
        jComboBoxExtended1 = new eSchool.GUI.Controls.JComboBoxExtended();
        jLabel1 = new javax.swing.JLabel();
        jComboBoxExtended2 = new eSchool.GUI.Controls.JComboBoxExtended();

        lblIdDisciplina.setText("Código"); // NOI18N

        lblDisciplina.setText("Nome"); // NOI18N

        jButton4.setText("Cancelar"); // NOI18N
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton3.setText("Gravar"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        lblDocente.setText("Docente Responsável"); // NOI18N

        jComboBoxExtended1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel1.setText("Docente");

        jComboBoxExtended2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblDocente)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblIdDisciplina)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtIdDisciplina, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblDisciplina)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txtDisciplina, javax.swing.GroupLayout.PREFERRED_SIZE, 329, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jComboBoxExtended1, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jComboBoxExtended2, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblIdDisciplina)
                    .addComponent(txtIdDisciplina, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblDisciplina)
                    .addComponent(txtDisciplina, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3))
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblDocente)
                    .addComponent(jComboBoxExtended1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jComboBoxExtended2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        eventListner.CancelButtonClickEvent(this, evt);
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        
        Boolean eValido = true;
        String mensagemErro = TextHelper.EmptyString;
        
        if(codObrigatorio && txtIdDisciplina.getText().equals(TextHelper.EmptyString)){
            eValido = false;
            mensagemErro = "O identificador é obrigatório. \n";
        }
        
        if(nomeObrigatorio && txtDisciplina.getText().equals(TextHelper.EmptyString)){
            eValido = false;
            mensagemErro += "O nome é obrigatório. \n";
        }
        
        if(eValido)
            eventListner.SaveButtonClickEvent(this, evt);
        else
            JOptionPane.showMessageDialog(this, mensagemErro);
        
    }//GEN-LAST:event_jButton3ActionPerformed
        
    public void setDataSource(TableModel tableModel){
        this.tableModel = tableModel;
    }
    
    public void setBindIdDisciplinaIndex(int idDisciplinaColumnIndex){
        this.idDisciplinaColumnIndex = idDisciplinaColumnIndex;
    }
    
    public void setBindDisciplinaIndex(int disciplinaColumnIndex){
        this.disciplinaColumnIndex = disciplinaColumnIndex;
    }
    
    public void DataBind() throws Exception{
        this.txtIdDisciplina.setText(TextHelper.toString(tableModel.getValueAt(0, idDisciplinaColumnIndex)));
        this.txtDisciplina.setText(TextHelper.toString(tableModel.getValueAt(0, disciplinaColumnIndex)));
        for(int i=0; i<jComboBoxExtended1.getCount(); i++){
            if(TextHelper.toString(jComboBoxExtended1.getValueAt(i)).equals(TextHelper.toString(tableModel.getValueAt(0, 3)))){
                jComboBoxExtended1.setSelectedIndex(i);
            }
            if(TextHelper.toString(jComboBoxExtended2.getValueAt(i)).equals(TextHelper.toString(tableModel.getValueAt(0, 2)))){
                jComboBoxExtended2.setSelectedIndex(i);
            }
        }
    }
        
    public void setListaDocentes(TableModel tableModel){
        jComboBoxExtended1.removeAllItems();
        jComboBoxExtended1.addItemsFrom(tableModel, 8 , 0);
        jComboBoxExtended2.removeAllItems();
        jComboBoxExtended2.addItemsFrom(tableModel, 8 , 0);
    }
        
    // <editor-fold defaultstate="collapsed" desc="Getters & Setters">                          
    public String getIDDisciplina(){
        return  txtIdDisciplina.getText();
    }
    
    public void setIDDisciplina(String idDisciplina){
        txtIdDisciplina.setText(idDisciplina);
    }

    public String getDisciplina(){
        return  txtDisciplina.getText();
    }
    
    public void setDisciplina(String disciplina){
        txtDisciplina.setText(disciplina);
    }
    
    public int getIdDocenteResponsavel(){
        return Integer.parseInt(jComboBoxExtended1.getSelectedValue().toString()); 
    }
    
    public int getIdDocente(){
        return Integer.parseInt(jComboBoxExtended2.getSelectedValue().toString()); 
    }
          
    public Boolean getCodObrigatorio() {
        return codObrigatorio;
    }

    public void setCodObrigatorio(Boolean codObrigatorio) {
        this.codObrigatorio = codObrigatorio;
    }

    public Boolean getNomeObrigatorio() {
        return nomeObrigatorio;
    }

    public void setNomeObrigatorio(Boolean nomeObrigatorio) {
        this.nomeObrigatorio = nomeObrigatorio;
    }
    // </editor-fold>  
        
    // <editor-fold defaultstate="collapsed" desc="Declaração de variáveis">                          
    TableModel tableModel;
    int idDisciplinaColumnIndex;
    int disciplinaColumnIndex;
    private Boolean codObrigatorio = false;
    private Boolean nomeObrigatorio = false;
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private eSchool.GUI.Controls.JComboBoxExtended jComboBoxExtended1;
    private eSchool.GUI.Controls.JComboBoxExtended jComboBoxExtended2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel lblDisciplina;
    private javax.swing.JLabel lblDocente;
    private javax.swing.JLabel lblIdDisciplina;
    private javax.swing.JTextField txtDisciplina;
    private javax.swing.JTextField txtIdDisciplina;
    // End of variables declaration//GEN-END:variables
 // </editor-fold>      
}
